package d5_NIO;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class t1Channel {
    public static void main(String[] args) throws Exception {
        RandomAccessFile raf = new RandomAccessFile("D:\\mycode\\file10\\src\\111","rw");
        FileChannel channel = raf.getChannel();//创建频道

        ByteBuffer buffer = ByteBuffer.allocate(1024);//

        int bytesRead = channel.read(buffer);
        while (bytesRead != -1) {
            System.out.println(bytesRead);
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.print((char) buffer.get());
            }
            buffer.clear();
            bytesRead = channel.read(buffer);
        }
        channel.close();
        System.out.println("test over");


    }
}
